//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ConstantYoYOptionletVolatility.h"
using namespace Cephei::QL::Termstructures::Volatility::Inflation;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Volatility/Inflation/YoYOptionletVolatilitySurface.h>
using namespace Cephei::QL::Times;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::CConstantYoYOptionletVolatility (Double v, UInt32 settlementDays, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Cephei::QL::Times::IDayCounter^ dc, Cephei::QL::Times::IPeriod^ observationLag, QL::Times::FrequencyEnum frequency, Boolean indexIsInterpolated, Microsoft::FSharp::Core::FSharpOption<Double>^ minStrike, Microsoft::FSharp::Core::FSharpOption<Double>^ maxStrike) : CYoYOptionletVolatilitySurface(CConstantYoYOptionletVolatility::typeid)
{
    CCalendar^ _Ccal;
    CDayCounter^ _Cdc;
    CPeriod^ _CobservationLag;
    try
    {
#ifdef HANDLE
        _phConstantYoYOptionletVolatility = NULL;
#endif
        QuantLib::Volatility _v = (QuantLib::Volatility)ValueHelper::Convert (v);
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays);
        _Ccal = safe_cast<CCalendar^> (cal);
        _Ccal->Lock();
        QuantLib::Calendar& _cal = static_cast<QuantLib::Calendar&> (_Ccal->GetReference ()); 
        QuantLib::BusinessDayConvention _bdc = (QuantLib::BusinessDayConvention)bdc ;
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        _CobservationLag = safe_cast<CPeriod^> (observationLag);
        _CobservationLag->Lock();
        QuantLib::Period& _observationLag = static_cast<QuantLib::Period&> (_CobservationLag->GetReference ()); 
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        bool _indexIsInterpolated = (bool)ValueHelper::Convert (indexIsInterpolated);
        QuantLib::Rate _minStrike = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (minStrike) ? (QuantLib::Rate)ValueHelper::Convert (minStrike->Value) : -1.0); //4
        QuantLib::Rate _maxStrike = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (maxStrike) ? (QuantLib::Rate)ValueHelper::Convert (maxStrike->Value) : 100.0); //4
        _ppConstantYoYOptionletVolatility = new boost::shared_ptr<QuantLib::ConstantYoYOptionletVolatility> (new QuantLib::ConstantYoYOptionletVolatility ( _v,  _settlementDays,  _cal,  _bdc,  _dc,  _observationLag,  _frequency,  _indexIsInterpolated,  _minStrike,  _maxStrike ));
        SetYoYOptionletVolatilitySurface (boost::dynamic_pointer_cast<QuantLib::YoYOptionletVolatilitySurface> (*_ppConstantYoYOptionletVolatility));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccal != nullptr) _Ccal->Unlock();
        if (_Cdc != nullptr) _Cdc->Unlock();
        if (_CobservationLag != nullptr) _CobservationLag->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::CConstantYoYOptionletVolatility (boost::shared_ptr<QuantLib::ConstantYoYOptionletVolatility>& childNative, Object^ owner) : CYoYOptionletVolatilitySurface(CConstantYoYOptionletVolatility::typeid)
{
#ifdef HANDLE
	_phConstantYoYOptionletVolatility = NULL;
#endif
	_ppConstantYoYOptionletVolatility = &childNative;
    _ppYoYOptionletVolatilitySurface = new boost::shared_ptr<QuantLib::YoYOptionletVolatilitySurface> (boost::dynamic_pointer_cast<QuantLib::YoYOptionletVolatilitySurface> (*_ppConstantYoYOptionletVolatility));
}
Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::CConstantYoYOptionletVolatility (QuantLib::ConstantYoYOptionletVolatility& childNative, Object^ owner) : CYoYOptionletVolatilitySurface(CConstantYoYOptionletVolatility::typeid)
{
#ifdef HANDLE
	_phConstantYoYOptionletVolatility = NULL;
#endif
	_ppConstantYoYOptionletVolatility = new boost::shared_ptr<QuantLib::ConstantYoYOptionletVolatility> (&childNative);
    _ppYoYOptionletVolatilitySurface = new boost::shared_ptr<QuantLib::YoYOptionletVolatilitySurface> (boost::dynamic_pointer_cast<QuantLib::YoYOptionletVolatilitySurface> (*_ppConstantYoYOptionletVolatility));
    _ConstantYoYOptionletVolatilityOwner = owner;
    _YoYOptionletVolatilitySurfaceOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::CConstantYoYOptionletVolatility (CConstantYoYOptionletVolatility^ copy) : CYoYOptionletVolatilitySurface(CConstantYoYOptionletVolatility::typeid)
{
#ifdef HANDLE
	_phConstantYoYOptionletVolatility = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppConstantYoYOptionletVolatility = new boost::shared_ptr<QuantLib::ConstantYoYOptionletVolatility> (copy->GetShared());
        _ppYoYOptionletVolatilitySurface = new boost::shared_ptr<QuantLib::YoYOptionletVolatilitySurface> (boost::dynamic_pointer_cast<QuantLib::YoYOptionletVolatilitySurface> (*_ppConstantYoYOptionletVolatility));
    }
}
Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::CConstantYoYOptionletVolatility (System::Type^ t) : CYoYOptionletVolatilitySurface(CConstantYoYOptionletVolatility::typeid)
{
#ifdef HANDLE
	_phConstantYoYOptionletVolatility = NULL;
#endif
	if (!t->IsSubclassOf(CConstantYoYOptionletVolatility::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::CConstantYoYOptionletVolatility (QuantLib::Handle<QuantLib::ConstantYoYOptionletVolatility>& childNative, Object^ owner)  : CYoYOptionletVolatilitySurface(CConstantYoYOptionletVolatility::typeid)
{
	_phConstantYoYOptionletVolatility = &childNative;
	_ppConstantYoYOptionletVolatility = &static_cast<boost::shared_ptr<QuantLib::ConstantYoYOptionletVolatility>>(childNative.currentLink());
    _ppYoYOptionletVolatilitySurface = new boost::shared_ptr<QuantLib::YoYOptionletVolatilitySurface> (boost::dynamic_pointer_cast<QuantLib::YoYOptionletVolatilitySurface> (*_ppConstantYoYOptionletVolatility));
    _ConstantYoYOptionletVolatilityOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::CConstantYoYOptionletVolatility (QuantLib::Handle<QuantLib::ConstantYoYOptionletVolatility> childNative)  : CYoYOptionletVolatilitySurface(CConstantYoYOptionletVolatility::typeid)
{
	_phConstantYoYOptionletVolatility = &childNative;
	_ppConstantYoYOptionletVolatility = &static_cast<boost::shared_ptr<QuantLib::ConstantYoYOptionletVolatility>>(childNative.currentLink());
    _ppYoYOptionletVolatilitySurface = new boost::shared_ptr<QuantLib::YoYOptionletVolatilitySurface> (boost::dynamic_pointer_cast<QuantLib::YoYOptionletVolatilitySurface> (*_ppConstantYoYOptionletVolatility));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::CConstantYoYOptionletVolatility (QuantLib::ConstantYoYOptionletVolatility childNative)  : CYoYOptionletVolatilitySurface(CConstantYoYOptionletVolatility::typeid)
{
#ifdef HANDLE
	_phConstantYoYOptionletVolatility = NULL;
#endif
	_ppConstantYoYOptionletVolatility = new boost::shared_ptr<QuantLib::ConstantYoYOptionletVolatility> (new QuantLib::ConstantYoYOptionletVolatility (childNative));
    _ppYoYOptionletVolatilitySurface = new boost::shared_ptr<QuantLib::YoYOptionletVolatilitySurface> (boost::dynamic_pointer_cast<QuantLib::YoYOptionletVolatilitySurface> (*_ppConstantYoYOptionletVolatility));
}
#endif

Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::~CConstantYoYOptionletVolatility ()
{
    if (_ppConstantYoYOptionletVolatility != NULL)
    {
	    delete _ppConstantYoYOptionletVolatility;
        _ppConstantYoYOptionletVolatility = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::!CConstantYoYOptionletVolatility ()
{
    if (_ppConstantYoYOptionletVolatility != NULL)
    {
	    delete _ppConstantYoYOptionletVolatility;
    }
}
QuantLib::ConstantYoYOptionletVolatility& Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::GetReference ()
{
    if (_ppConstantYoYOptionletVolatility == NULL) throw gcnew NativeNullException ();
	return **_ppConstantYoYOptionletVolatility;
}
boost::shared_ptr<QuantLib::ConstantYoYOptionletVolatility>& Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::GetShared ()
{
    if (_ppConstantYoYOptionletVolatility == NULL) throw gcnew NativeNullException ();
	return *_ppConstantYoYOptionletVolatility;
}
QuantLib::ConstantYoYOptionletVolatility* Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::GetPointer ()
{
    if (_ppConstantYoYOptionletVolatility == NULL) throw gcnew NativeNullException ();
	return &**_ppConstantYoYOptionletVolatility;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ConstantYoYOptionletVolatility>& Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::GetHandle ()
{
	if (_phConstantYoYOptionletVolatility == NULL)
	{
		_phConstantYoYOptionletVolatility = new Handle<QuantLib::ConstantYoYOptionletVolatility> (*_ppConstantYoYOptionletVolatility);
	}
	return *_phConstantYoYOptionletVolatility;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::HasNative () 
{
	return (_ppConstantYoYOptionletVolatility != NULL);
}

DateTime Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppConstantYoYOptionletVolatility)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppConstantYoYOptionletVolatility)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppConstantYoYOptionletVolatility)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Inflation::IConstantYoYOptionletVolatility^ Cephei::QL::Termstructures::Volatility::Inflation::CConstantYoYOptionletVolatility_Factory::Create (Double v, UInt32 settlementDays, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Cephei::QL::Times::IDayCounter^ dc, Cephei::QL::Times::IPeriod^ observationLag, QL::Times::FrequencyEnum frequency, Boolean indexIsInterpolated, Microsoft::FSharp::Core::FSharpOption<Double>^ minStrike, Microsoft::FSharp::Core::FSharpOption<Double>^ maxStrike)
{
    return gcnew CConstantYoYOptionletVolatility ( v,  settlementDays,  cal,  bdc,  dc,  observationLag,  frequency,  indexIsInterpolated,  minStrike,  maxStrike);
}
